import {
  getPlaylist,
  getPlaylistCat,
  getPlaylistDetail,
  getPlaylistComment,
  getLovePeople
} from '@/services/playlist-api'
import * as actionTypes from './constants'
import {
  handleCategoryData
} from '@/utils/handle'
const changePlaylistAction = playlist => ({
  type: actionTypes.CHANGE_PLAYLIST_ACTION,
  playlist
})
const changePlaylistTotalCount = totalCount => ({
  type: actionTypes.CHANGE_PLAYLIST_TOTAL_COUNT_ACTION,
  totalCount
})
export const getPlaylistAction = () => {
  return async (dispatch, getState) => {
    const currentCat = getState().getIn(['playlist', 'currentCat'])
    const currentPage = getState().getIn(['playlist', 'currentPage'])
    const currentOrder = getState().getIn(['playlist','currentOrder'])
    const currentOffset = (currentPage - 1) * 35
    const res = await getPlaylist(currentCat, currentOrder, currentOffset)
    console.log(res);
    dispatch(changePlaylistAction(res.playlists))
    dispatch(changePlaylistTotalCount(res.total))
  }
}
const changeCurrentPageAction = currentPage => ({
  type: actionTypes.CHANGE_CURRENT_PAGE_ACTION,
  currentPage
})
export const getCurrentPageAction = page => {
  return dispatch => {
    dispatch(changeCurrentPageAction(page))
  }
}
const changePlaylistCatAction = catList => ({
  type: actionTypes.CHANGE_PLAYLIST_CAT_ACTION,
  catList
})
export const getPlaylistCatAction = () => {
  return async dispatch => {
    const res = await getPlaylistCat()
    console.log(handleCategoryData(res));
    dispatch(changePlaylistCatAction(handleCategoryData(res)))
  }
}
const changeCurrentCatAction = currentCat => ({
  type: actionTypes.CHANGE_CURRENT_CAT_ACTION,
  currentCat
})
export const getCurrentCatAction = currentCat => {
  return dispatch => {
    dispatch(changeCurrentCatAction(currentCat))
  }
}
const changePlaylistDetailAction = playlistDetail => ({
  type: actionTypes.CHANGE_PLAYLIST_DETAIL_ACTION,
  playlistDetail
})

export const getPlaylistDetailAction = id => {
  return async dispatch => {
    const res = await getPlaylistDetail(id)
    dispatch(changePlaylistDetailAction(res.playlist))
  }
}
const changePlaylistCommentAction = commentList => ({
  type: actionTypes.CHANGE_PLAYLIST_COMMENT_ACTION,
  commentList
})
const changePlaylistCommentCountAction = totalCommentCount => ({
  type: actionTypes.CHANGE_COMMENT_COUNT_ACTION,
  totalCommentCount
})
export const getPlaylistCommentAction = id => {
  return async (dispatch, getState) => {
    const currentCommentPage = getState().getIn(['playlist', 'currentCommentPage'])
    const offset = (currentCommentPage-1)*20
    const res = await getPlaylistComment(id, offset)
    console.log(res);
    dispatch(changePlaylistCommentCountAction(res.total))
    dispatch(changePlaylistCommentAction(res.comments))
  }
}
const changePlaylistCurrentCommentPageAction = currentCommentPage => ({
  type: actionTypes.CHANGE_CURRENT_COMMENT_PAGE_ACTION,
  currentCommentPage
})
export const getPlaylistCurrentCommentPageAction = (page) => {
  return dispatch => {
    dispatch(changePlaylistCurrentCommentPageAction(page))
  }
}
const changeLovePeoplesAction = lovePeoples => ({
  type: actionTypes.CHANGE_LOVE_PEOPLE_ACTION,
  lovePeoples
})
export const getLovePeoplesAction = id => {
  return async dispatch => {
    const res = await getLovePeople(id)
    console.log(res);
    dispatch(changeLovePeoplesAction(res.subscribers))
  }
}